package logic

import (
	"fmt"
	"msems/dao/mysql"
	"msems/models"
)

const (
	StringInsert = "insert"
	StringDelete = "delete"
	StringUpdate = "update"
)

// AdminLevel  获取用户id并判断权限是否足够
func AdminLevel(id int64) (level int, err error) {
	return mysql.AdminLevel(id)
}

// AdminLog 记录日志
func AdminLog(id int64, opera, mes string) (err error) {
	ope := new(models.AdminLog)
	ope = &models.AdminLog{
		AdminID:   id,
		Operation: opera,
		Message:   mes,
	}
	err = mysql.AdminLog(ope)
	if err != nil {
		return err
	}
	return
}

// ChangeCol 插入学院
func ChangeCol(p *models.ParamCollege, id int64) (err error) {
	if p.Operation == StringInsert {
		var cid int64
		//插入 学院数据
		cid, err = mysql.InsertCol(p)
		if err != nil {
			return err
		}
		//记录行为日志
		return AdminLog(id, StringInsert, fmt.Sprintf("id:%d,name:%s", cid, p.Name))
	} else if p.Operation == StringUpdate {
		//修改 学院数据
		err = mysql.UpdateCol(p)
		if err != nil {
			return err
		}
		//记录行为日志
		return AdminLog(id, StringUpdate, fmt.Sprintf("id:%d,name:%s", p.ID, p.Name))
	} else if p.Operation == StringDelete {
		//删除 学院数据
		err = mysql.DeleteCol(p)
		if err != nil {
			return err
		}
		//记录行为日志
		return AdminLog(id, StringDelete, fmt.Sprintf("id:%d,name:%s", p.ID, p.Name))
	}
	return
}

// GetColAll 获取所有学院
func GetColAll() (college []*models.College, err error) {
	return mysql.GetColAll()
}
